<template>
  <div class="nav-menu">
    <div class="logo">
      <img src="~assets/img/logo.svg" alt="logo" />
      <span class="title" v-if="!isCollapse">Vue3+TS</span>
    </div>
    <el-menu
      :default-active="defaultValue"
      class="el-menu-vertical"
      background-color="#001529"
      text-color="#fff"
      active-text-color="#0a60bd"
      :collapse="isCollapse"
    >
      <!-- 一级菜单 -->
      <template v-for="item in userMenus" :key="item.id">
        <template v-if="item.type === 1">
          <el-sub-menu :index="'' + item.id">
            <template #title>
              <el-icon>
                <component :is="formateIcon(item.icon)" />
              </el-icon>
              <span>{{ item.name }}</span>
            </template>
            <template v-for="subitem in item.children" :key="subitem.id">
              <!-- 二级菜单 -->
              <el-menu-item
                :index="'' + subitem.id"
                @click="handleMenuItemClick(subitem)"
              >
                <template #title>
                  <span>{{ subitem.name }}</span>
                </template>
              </el-menu-item>
            </template>
          </el-sub-menu>
        </template>
        <template v-else-if="item.type === 2">
          <el-menu-item
            :index="'' + item.id"
            @click="handleMenuItemClick(item)"
          >
            <template #title>
              <el-icon> <component :is="formateIcon(item.icon)" /></el-icon>
              <span>{{ item.name }}</span>
            </template>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref } from 'vue'
import { useStore } from '@/store/index'
import { formateIcon } from 'utils/formate'
import { pathMapToMenu } from 'utils/mapMenus'
import { useRoute, useRouter } from 'vue-router'
export default defineComponent({
  props: {
    isCollapse: {
      type: Boolean,
      default: false
    }
  },
  setup() {
    // store
    const store = useStore()
    const userMenus = computed(() => store.state.login.userMenus)
    // router
    const router = useRouter()
    const route = useRoute()
    // data
    const currentPath = route.path

    const menu = pathMapToMenu(userMenus.value, currentPath)

    const defaultValue = ref(menu?.id + '')

    // event handle
    const handleMenuItemClick = (item: any) => {
      router.push({
        path: item.url ?? '/not-found'
      })
    }
    return {
      userMenus,
      formateIcon,
      defaultValue,
      handleMenuItemClick
    }
  }
})
</script>

<style lang="scss" scoped>
.nav-menu {
  height: 100%;
  background-color: #001529;

  .logo {
    display: flex;
    height: 28px;
    padding: 12px 10px 8px 10px;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;

    img {
      height: 100%;
      margin: 0 10px;
    }

    .title {
      font-size: 16px;
      font-weight: 700;
      color: white;
    }
  }
  .el-menu {
    border-right: none;
  }
  .el-menu-item:hover {
    background: cornflowerblue !important;
  }
}

.el-menu-vertical:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}

::v-deep(.el-menu--inline) {
  background: #0a2032 !important;
}
</style>
